﻿<Serializable()>
Public Class TypeCode
    Implements IEquatable(Of TypeCode)

    Public Property ObjectType As ObjectType = ObjectType.ALL
    Public Property ObjectSubType As ObjectSubType = ObjectSubType.ALL

    Public Sub New()

    End Sub

    Public Sub New(objectType As ObjectType, objectSubType As ObjectSubType)
        Me.ObjectType = objectType
        Me.ObjectSubType = objectSubType
    End Sub

    Public Overrides Function ToString() As String
        Return ObjectType.ToString & ":" & ObjectSubType.ToString
    End Function

    Public Overrides Function Equals(obj As Object) As Boolean
        If TypeOf obj Is TypeCode Then
            Dim other As TypeCode = DirectCast(obj, TypeCode)
            If Me.ObjectType = other.ObjectType AndAlso Me.ObjectSubType = other.ObjectSubType Then
                Return True
            Else
                Return False
            End If
        Else
            Return False
        End If
    End Function

    Private Function Equals1(other As TypeCode) As Boolean Implements System.IEquatable(Of TypeCode).Equals
        Return Equals(other)
    End Function

    Public Overrides Function GetHashCode() As Integer
        Return Me.ToString.GetHashCode
    End Function

    Public Function Matches(other As TypeCode) As Boolean
        If Me.Equals(other) Then
            Return True
        End If
        If Me.ObjectType = ObjectType.ALL AndAlso Me.ObjectSubType = ObjectSubType.ALL Then
            Return True
        End If
        If Me.ObjectType = other.ObjectType AndAlso Me.ObjectSubType = ObjectSubType.ALL Then
            Return True
        End If
        Return False
    End Function

    Public Shared Operator =(obj1 As TypeCode, obj2 As TypeCode) As Boolean
        Return obj1.Equals1(obj2)
    End Operator

    Public Shared Operator <>(obj1 As TypeCode, obj2 As TypeCode) As Boolean
        Return Not obj1.Equals1(obj2)
    End Operator

    Function [is](objectType As ObjectType, objectSubType As ObjectSubType) As Boolean
        Return Me = New TypeCode(objectType, objectSubType)
    End Function

End Class
